﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.ComponentModel;
using V2TEK.NET.SH.Silverlight.Charts.GanttHelper;

namespace V2TEK.NET.SH.Silverlight.Charts.TreeGridHelper
{
    public abstract class AbstractExpander : Control, IExpander, INotifyPropertyChanged
    {
        public static DependencyProperty RowProperty = DependencyProperty.Register("Row", typeof(DataGridRow), typeof(AbstractExpander), null);
        public static DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(AbstractExpander), null);
        public static DependencyProperty LevelProperty = DependencyProperty.Register("Level", typeof(int), typeof(AbstractExpander), null);

        public const string DATA_PROPERTY = "DataContext";

        public AbstractExpander()
        {
        }

        private bool _isExpand;
        public virtual bool IsExpand
        {
            get
            {
                return _isExpand;
            }
            set
            {
                if (_isExpand != value)
                {
                    _isExpand = value;
                    if (this.Data != null && this.Data is TaskEntryLayoutItem)
                    {
                        ((TaskEntryLayoutItem)this.Data).IsExpand = value;
                    }
                }
            }
        }

        public object Data
        {
            get
            {
                return this.DataContext;
            }
            set
            {
                //object dc = this.DataContext;
                this.DataContext = value;                
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(DATA_PROPERTY));
            }
        }

        public DataGridRow Row
        {
            get
            {
                return (DataGridRow)GetValue(RowProperty);
            }
            set
            {
                SetValue(RowProperty, value);
            }
        }

        public object Content
        {
            get
            {
                return GetValue(ContentProperty);
            }
            set
            {
                SetValue(ContentProperty, value);
            }
        }

        public int Level
        {
            get
            {
                return (int)GetValue(LevelProperty);
            }
            set
            {
                SetValue(LevelProperty, value);
            }
        }

        protected void RaiseExpandChange(bool isExpanded)
        {
            if (ExpandChange != null)
                ExpandChange(this, new RowExpandEventArgs(Data, isExpanded, this));

        }

        public abstract void Update();

        #region IExpander Members

        public event ExpandChangeEventHandler ExpandChange;

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
